package com.kd.sms.config;

import com.kd.sms.ISmsProvider;
import com.kd.sms.provider.aliyun.AliYunSmsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class AutoConfiguration {

    @Bean
    public ISmsProvider getSmsProvider(@Autowired SmsProperties smsProperties, @Autowired ApplicationEventPublisher applicationEventPublisher)  throws Exception {
        if (smsProperties.getProvider() == null) {
            return new AliYunSmsProvider(smsProperties, applicationEventPublisher);
        }

        return (ISmsProvider) smsProperties.getProvider().getDeclaredConstructor(SmsProperties.class, ApplicationEventPublisher.class).newInstance(smsProperties, applicationEventPublisher);
    }
}
